package com.doyle.musicplus.shared;

import com.google.gwt.dev.util.collect.HashMap;

public class KeyValueMap extends HashMap<String, String> {
	
	private static final long serialVersionUID = 52257837383688L;
	
	public KeyValueMap() {
		this("");
	}
	
	public KeyValueMap(final String params) {
		initializeWithString(params);
	}

	void initializeWithString(String params) {
		
		clear();
		if ((params != null) && !params.isEmpty()) {
			String[] args = params.split("&");
			for (String element : args) {
				int equalIndex = element.indexOf("=");
				if (equalIndex == -1) {
					put(element, "");
				} else {
					put(element.substring(0, equalIndex), element.substring(equalIndex + 1));
				}
			}
		}
		
	}
	
	public String toString() {
		
		String result = "";
		String separator = "";
		for (String key : keySet()) {
			result += separator + key + "=" + get(key);
			separator = "\n";
		}
		return result;
	}
	
}
